import requests
import time
sess=requests.session()
Ip='192.168.1.180'

# 1.表单上传接口	POST	/pinter/file/api/upload	文件类型
def file_upload():
    # 传入文件的路径
    path='D:\\1.jpg'
    # 打开文件 并传入files 变量中
    with open(path,"rb") as f:
        # file 是fiddler抓包中 name属性的值
        files={'file':(path,f)}
    # 文件上传
        rp=sess.request('post', url='http://' + Ip + ':8080/pinter/file/api/upload',files=files)
        print(rp.text)

# 2.Ajax上传接口	POST	/pinter/file/api/upload2	文件类型
def file_ajax_upload():
    # 传入文件的路径
    path='D:\\1.jpg'
    # 打开文件并传入file 变量中
    with open(path,"rb") as f:
        # file 是fiddler抓包中 name属性的值
        files={'file':(path,f)}
    # 文件上传
        rp=sess.request('post', url='http://' + Ip + ':8080/pinter/file/api/upload2',files=files)
        print(rp.text)

# 3.文件下载接口	GET	/pinter/file/api/download	id=1
def file_download(id):
    # 传入下载文件的编号
    param = {'id': id}
    # 文件下载

    timestr = time.strftime("%Y-%m-%d_%H-%M-%S", time.localtime())
    rp = sess.request('get', url='http://' + Ip + ':8080/pinter/file/api/download', params=param)
    with open(f"d:\\{id}.{timestr}", 'wb') as f:
        f.write(rp.content)
    print(rp.text)
if __name__=='__main__':
    file_upload()
    file_ajax_upload()
    file_download(1)



